/*
 *@Author: 张楚
 *@Date: 2021-08-12 10:13:29
 *@Description: dismissible 滑动删除组件
*/

import 'package:flutter/material.dart';
import 'package:material_dialogs/material_dialogs.dart';
import 'package:get/get.dart';

class DissmissiblePage extends StatefulWidget {
  const DissmissiblePage({Key key}) : super(key: key);
  @override
  _DissmissiblePageState createState() => _DissmissiblePageState();
}

class _DissmissiblePageState extends State<DissmissiblePage> {
  bool isDel = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('滑动删除'),
        ),
        body: ListView.builder(
            itemCount: 100,
            itemBuilder: (_, index) {
              return Dismissible(
                  key: UniqueKey(),
                  //滑动的时候，产生回调函数
                  //一般用于更新用户逻辑
                  //表面上只是ui的改变
                  onDismissed: (v) {
                    print(v);
                    if (v == DismissDirection.endToStart) {
                      print('从后往前滑');
                    }
                  },
                  //确认是否删除
                  confirmDismiss: (v) async {
                    // d();
                    return true;
                  },
                  //确认删除的时候，开始消失的时候，就开始做的一系列操作
                  onResize: () {
                    print('逐渐慢慢消失');
                  },
                  //确认消失之后，消失的动画时间长度
                  resizeDuration: Duration(seconds: 1),
                  //动画滑动的时间
                  movementDuration: Duration(seconds: 2),
                  //
                  dismissThresholds: {
                    DismissDirection.startToEnd: 0.1, //从zuo
                    DismissDirection.endToStart: 0.8
                  },
                  //左滑的颜色
                  background: Container(
                    color: Colors.white,
                    padding: EdgeInsets.only(left: 10),
                    alignment: Alignment.centerLeft,
                    child: Icon(Icons.dangerous),
                  ),
                  //右滑的颜色
                  secondaryBackground: Container(
                    padding: EdgeInsets.only(right: 10),
                    alignment: Alignment.centerRight,
                    color: Colors.black,
                    child: Icon(
                      Icons.phone,
                      color: Colors.green,
                    ),
                  ),
                  child: Container(
                    height: 50,
                    color: Colors.orange[index % 9 * 100],
                  ));
            }));
  }

  bool d() {
    Dialogs.materialDialog(
        context: context,
        msg: '你确定要删除吗',
        title: '删除',
        color: Colors.white,
        actions: [
          TextButton(
              child: Text('不删除'),
              onPressed: () {
                setState(() {});
                isDel = false;
                Get.back();
              }),
          TextButton(
              child: Text('删除'),
              onPressed: () {
                setState(() {
                  isDel = true;
                });
                Get.back();
              })
        ]);
    return isDel;
  }
}
